﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WareDealer;

namespace KillPrice
{
    public partial class UiHisdataImport : XtraUserControl
    {
        public UiHisdataImport()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化Ui元素
        /// </summary>
        private void InitUiAtom()
        {
            progressImport.Visible = false;
            txtMsg.Text = "";
            btnChoseDBFile.Text = "";
            _isImport = false;
        }

        private void btnChoseDBFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            openFileDialog1.Multiselect = false;
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                btnChoseDBFile.Text = fileName;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(btnChoseDBFile.Text.Trim()))
            {
                MessageBox.Show("请检查导入文件路径是否正确！","系统提示");
                return;
            }
            if (!System.IO.File.Exists(btnChoseDBFile.Text))
            {
                return;
            }
            string fileName = btnChoseDBFile.Text;

            Thread exportThread = new Thread(delegate() { ImportHisWareThread(fileName); }) { Name = "importHisThread", IsBackground = true };
            exportThread.Start();

            _isImport = true;
            btnImport.Text = "导入中";
            btnImport.Enabled = false;
        }

        #region ImportProcess
        /// <summary>
        /// 导入历史数据处理线程
        /// </summary>
        /// <param name="dbFilePath"></param>
        private void ImportHisWareThread(string dbFilePath)
        {
            JdListener.GetInstance().ProcessBarIDs = new List<string>(){"main" };
            JdListener.GetInstance().InitProcess = InitImportProcess;
            JdListener.GetInstance().ShowStep = ShowImportStep;
            JdListener.GetInstance().EndProcess = EndImportProcess;
            JdListener.GetInstance().ShowMessage = ShowImportMsg;
            JdListener.GetInstance().ImportHisDataAction(dbFilePath);
        }
        /// <summary>
        /// 是否正在导入 导入时不允许退出当前窗体
        /// </summary>
        private bool _isImport = false;
        private bool _isProcessing = false;

        private void InitImportProcess(string processID, int iMax)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<string, int>(InitImportProcess), processID, iMax);
            }
            else
            {
                //设置一个最小值
                progressImport.Properties.Minimum = 0;
                //设置一个最大值
                progressImport.Properties.Maximum = iMax;
                //设置步长，即每次增加的数
                progressImport.Properties.Step = 1;
                //当前位置
                progressImport.Position = 0;
                //显示数字
                progressImport.Properties.ShowTitle = true;
                //设置进度条的样式
                progressImport.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid;
                progressImport.Visible = true;
                _isProcessing = true;
            }
        }

        private void ShowImportStep(string processID, int iStep)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string, int>(ShowImportStep), processID, iStep);
            }
            else
            {
                //主要用于对进度条进行进度控制
                if (_isProcessing)
                {
                    if (iStep <= progressImport.Properties.Maximum)
                    {
                        //处理当前消息队列中的所有windows消息
                        Application.DoEvents();
                        progressImport.Text = iStep.ToString();
                        progressImport.EditValue = iStep;
                        //执行步长
                        //progressBarControl1.PerformStep();
                        progressImport.Update();
                    }
                }
            }
        }

        private void ShowImportMsg(string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(ShowImportMsg), msg);
            }
            else
            {
                txtMsg.Text = txtMsg.Text + (DateTime.Now.ToString() + " " + msg + "\r\n");
                txtMsg.SelectionStart = txtMsg.Text.Length;
                txtMsg.ScrollToCaret();
            }
        }

        private void EndImportProcess(string processID, bool bRtn)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string,bool>(EndImportProcess), processID, bRtn);
            }
            else
            {
                _isImport = !bRtn;
                btnImport.Text = "导入完成";
                btnImport.Enabled = false;
            }

        }

        #endregion ImportProcess

        private void UiHisdataImport_Load(object sender, EventArgs e)
        {
            InitUiAtom();
        }
    }
}
